/* Copyright (C) 2008, 2009, 2010, Faindu GmbH, Switzerland.
 * All Rights Reserved. The following is Source Code and is subject to all restrictions 
 * on such code as contained in the license accompanying this project. */
package blocstac.api.http
{

public class MimeType
{
	public function MimeType(mimeTypeString:String) {
		_mimeType = mimeTypeString
	}

	public static function createFromFileName(fileName:String):MimeType {
		var mimeTypeString:String = suffixToMimetype(fileName)
		return new MimeType(mimeTypeString)
	}
	
	/**
	 * Convert a file suffix to a mimetype.
	 *  
	 * @param suffixOrName is either a suffix such as "jpg", "doc", "pdf" or a filename such as
	 * "hugo.jpg" or "plan.doc". "jpg" is the same as "JpG" or "JPG".
	 * @return the mimetype that relates to the suffix, e.g. "image/gif". If no suitable
	 * mimetype is found, "" is returned.
	 */
	private static function suffixToMimetype(suffixOrName:String): String {
		if ( suffixOrName ) {
			suffixOrName = suffixOrName.toLowerCase()
			var suffix: String
			var index:int = suffixOrName.lastIndexOf(".")
			if (index != -1) {
				suffix = suffixOrName.substring(index+1, suffixOrName.length)
			} else {
				suffix = suffixOrName	
			}
			
			switch (suffix) {
				case SUFFIX_GIF:
					return MIMETYPE_GIF
				 		
				case SUFFIX_JPG:
				case SUFFIX_JPEG:
					return MIMETYPE_JPG
				 		
				case SUFFIX_PNG:
					return MIMETYPE_PNG
					
				case SUFFIX_DOC:
					return MIMETYPE_DOC
					
				case SUFFIX_DOCX:
					return MIMETYPE_DOCX
					
				case SUFFIX_XLS:
					return MIMETYPE_XLS
					
				case SUFFIX_XLSX:
					return MIMETYPE_XLSX
					
				case SUFFIX_PPT:
					return MIMETYPE_PPT
					
				case SUFFIX_PPTX:
					return MIMETYPE_PPTX
					
				case SUFFIX_PDF:
					return MIMETYPE_PDF
			}
		}
		return ""
	}
	
    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------
	private var _mimeType:String
	private var _fileSuffix:String
	
    //==========================================================================
    //
    //  MIME types and suffix
    //
    //--------------------------------------------------------------------------		
	// see http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types for complete list
	
	public static const MIMETYPE_ATOM: String = "application/atom+xml"

	private static const MIMETYPE_DEFAULT: String = "UTF-8"
	
	public static const MIMETYPE_TEXT: String = "text/plain"
	public static const MIMETYPE_HTML: String = "text/html"
	
	
	public static const MIMETYPE_GIF: String = "image/gif"
	private static const SUFFIX_GIF: String = "gif"

	public static const MIMETYPE_JPG: String = "image/jpeg"
	private static const SUFFIX_JPG: String = "jpg"
	private static const SUFFIX_JPEG: String = "jpeg"
	
	public static const MIMETYPE_PNG: String = "image/png"
	private static const SUFFIX_PNG: String = "png"

	public static const MIMETYPE_DOC: String = "application/msword"
	public static const MIMETYPE_DOCVND: String = "application/vnd.ms-word" 
	private static const SUFFIX_DOC: String = "doc"
	public static const MIMETYPE_DOCX: String = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
	private static const SUFFIX_DOCX: String = "docx"

	public static const MIMETYPE_XLS: String = "application/msexcel"
	public static const MIMETYPE_XLSVND: String = "application/vnd.ms-excel"
	private static const SUFFIX_XLS: String = "xls"
	public static const MIMETYPE_XLSX: String = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
	private static const SUFFIX_XLSX: String = "xlsx"

	public static const MIMETYPE_PPT: String = "application/mspowerpoint"
	public static const MIMETYPE_PPTVND: String = "application/vnd.ms-powerpoint"
	private static const SUFFIX_PPT: String = "ppt"
	public static const MIMETYPE_PPTX: String = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
	private static const SUFFIX_PPTX: String = "pptx"

	public static const MIMETYPE_PDF: String = "application/pdf"
	private static const SUFFIX_PDF: String = "pdf"

	public static const SUFFIX_XML: String = "xml"
	
    //==========================================================================
    //
    //  Specific mime types
    //
    //--------------------------------------------------------------------------	
	public function get isWord():Boolean {
		return _mimeType == MIMETYPE_DOC || _mimeType == MIMETYPE_DOCVND || _mimeType == MIMETYPE_DOCX
	}
	
	public function get isExcel():Boolean {
		return _mimeType == MIMETYPE_XLS || _mimeType == MIMETYPE_XLSVND || _mimeType == MIMETYPE_XLSX
	}
	
	public function get isPowerpoint():Boolean {
		return _mimeType == MIMETYPE_PPT || _mimeType == MIMETYPE_PPTVND || _mimeType == MIMETYPE_PPTX
	}

	public function get isPdf():Boolean {
		return _mimeType == MIMETYPE_PDF
	}
	
	/**
	 * Check if <code>mimetype</code> indicates an image.
	 * 
	 * @param mimetype
	 * @return true when mimetype is either a gif, jpg or png image. 
	 */
	public function get isImage(): Boolean {
		if ( 	_mimeType && ( 
				_mimeType == MIMETYPE_GIF ||
				_mimeType == MIMETYPE_JPG ||
				_mimeType == MIMETYPE_PNG ) )
		{
			return true
		}
		return false
	}
	
	public function toString():String {
		return _mimeType
	}

}
}